package com.yunxin.download.server.infra.support.filed.transfer.impl;

import com.yunxin.download.server.service.extenal.FileExportConfigDTO;
import com.yunxin.download.server.infra.support.filed.transfer.FieldTransfer;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * @author zero°
 * @since 2023-06-15
 */
@Slf4j
public class KvDataTransfer implements FieldTransfer<Object, Object> {
    @Override
    public Object transfer(Object value, FileExportConfigDTO.FieldFormat format) {
        final Map<String, Object> param = format.getParam();
        final Object data = param.get("data");
        final Object defaultValue = param.get("defaultValue");
        value = value == null ? "" : value.toString();
        if (data != null) {
            final Map<String, Object> map = (Map<String, Object>) data;
            if (map.containsKey(value)) {
                return map.get(value);
            }
        } else {
            log.warn("没有键值对数据");
        }
        return defaultValue == null ? "" : defaultValue;
    }
}
